import json

import tornado.web
import tornado.httpclient


class ReturnObj(object):
    def __init__(self, code, msg, data):
        self.code = code
        self.msg = msg
        self.data = data

    def to_json(self):
        return json.dumps(self, default=lambda o: o.__dict__, ensure_ascii=False)


class BaseHandler(tornado.web.RequestHandler):

    def set_default_headers(self):
        origin_url = self.request.headers.get('Origin')
        if origin_url:
            self.set_header("Access-Control-Allow-Origin", origin_url)
            self.set_header("Access-Control-Allow-Credentials", "true")
            self.set_header("Access-Control-Allow-Headers", "Content-Type")
            self.set_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")

    def options(self):
        # 处理预检请求（OPTIONS 请求）
        self.set_status(204)  # No Content
        self.finish()
